// pages/register/register.js
import config from "../../config.js";
const openIdUrl = config;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgImg: "https://app.longchg.com/gudongcar/images/loginbg.png",
    isGetCode: false,
    sendSatus: "发送验证码",
    time: 60, //验证码间隔



    // 注册信息
    telephone: '',
    verifyCode: '',
    openId: '',
    password: '',
    passwordConfirm: '',



  },

  bindTel: function(e) {
    var value = e.detail.value
    var pos = e.detail.cursor
    console.log(value);
    this.setData({
      telephone: value
    })
  },

  bindCode: function(e) {
    var value = e.detail.value
    var pos = e.detail.cursor
    console.log(value);
    this.setData({
      verifyCode: value
    })
  },

  bindPassword: function(e) {
    var value = e.detail.value
    var pos = e.detail.cursor
    console.log(value);
    this.setData({
      password: value
    })
  },

  bindPasswordConfirm: function(e) {
    var value = e.detail.value
    var pos = e.detail.cursor
    console.log(value);
    this.setData({
      passwordConfirm: value
    })
  },


  //手机号校验
  telCheck: function(tel) {
    var tel = tel,
      reg = /^1[123456789]\d{9}$/;
    if (tel == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (tel.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        mask: true,
        duration: 1500
      })
      return false;
    }
    if (!reg.test(tel)) {
      wx.showToast({
        title: '手机号格式有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    return true;
  },


  //验证码按钮状态
  changeCodeStatus: function() {
    var flag = this.data.isGetCode,
      that = this;

    var timer = setInterval(function() {
      if (that.data.time > 0) {
        that.setData({
          time: (that.data.time - 1),
        })
        that.setData({
          sendSatus: that.data.time + "秒后重新获取",
        })
      } else {
        clearInterval(timer);
        that.setData({
          time: 60, //验证码间隔
          isGetCode: false
        })
        that.setData({
          sendSatus: "请获取验证码",
        })
      }
    }, 1000);
  },


  //发送验证码
  getCode: function() {
    var tel = this.data.telephone,
      flag_tel = this.telCheck(tel),
      flag_getting = this.data.isGetCode,
      that = this;
    //手机号格式不对或者正在发送验证码时返回
    if (!flag_tel || flag_getting) return false;

    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.getVerifyCode,
      header: {
        'content-type': 'application/json',
      },
      data: {
        telephone: tel,
        type: 0
      },
      success: function(res) {
        console.log(res);
        if (res.data.status == 200) {

          //更改倒计时；
          that.setData({
            isGetCode: true
          })
          that.changeCodeStatus();
          wx.setStorageSync("session", res.data.data);
        }

        wx.hideLoading();

        wx.showToast({
          //title: res.data.message + "",
          title: res.data.message,
          // image:'../../vendor/assets/images/logo.png',
          icon: 'none',
          duration: 2000
        })

      }
    })
    return true;
  },



  // 注册
  submit: function() {
    var telephone = this.data.telephone,
      verifyCode = this.data.verifyCode,
      password = this.data.password,
      passwordConfirm = this.data.passwordConfirm;
    var flag_tel = this.telCheck(telephone);
    if (flag_tel) {
      if (verifyCode == '') {
        wx.showToast({
          title: '请输入验证码',
          icon: 'none',
          mask: true,
          duration: 1500
        })
        return false;
      }
      if (password == '') {
        wx.showToast({
          title: '请输入密码',
          icon: 'none',
          mask: true,
          duration: 1500
        })
        return false;
      }
      if (password != passwordConfirm) {
        wx.showToast({
          title: '两次输入密码不一致',
          icon: 'none',
          mask: true,
          duration: 1500
        })
        return false;
      }

      wx.request({
        url: openIdUrl.changePwdUrl,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': wx.getStorageSync("session")
        },
        data: {
          telephone: telephone,
          verifyCode: verifyCode,
          model: wx.getStorageSync('model'),
          openId: wx.getStorageSync('openID'),
          password: password,
        },
        dataType: 'json',
        success: function(res) {
          
          console.log(res);
          if(res.data.status==200){
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              mask: true,
              duration: 1500
            })

            console.log(res);
            setTimeout(function () {
              wx.navigateBack({
                
              })
            }, 2000);
          }
          if (res.statusCode==404){
            wx.showToast({
              title: "请求失败，请检查网络",
              icon: 'none',
              mask: true,
              duration: 1500
            })
          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              mask: true,
              duration: 1500
            })
          }

        },
       fail:function(error,resr,res){
         
           console.log("error");
         
       }
      })
    }
  },
  agreementDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    var url = config.http + "://" + config.host + "/agreemmentContent.action?contentId=" + id;
    wx.setStorageSync("webviewUrl", url);
    wx.navigateTo({
      url: "../webview/webview"
    })
  }




})